<?php

/**
 * cron actions.
 *
 * @package    reduceri-cadouri
 * @subpackage cron
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class cronActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    set_time_limit(0);
    ini_set("memory_limit", "128M");
    
    if($request->getParameter('id') == 'dw934hj34d83003xs')
    {
      $start_time = time();
      
      $sites = SitePeer::doSelect(new Criteria());
      $site_offers = array();

      foreach($sites as $site)
      {
        $site_offers[$site->getId()] = array();
        switch($site->getId())
        {
          case 1: // fundeal
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseFundeal($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save();
                  
                  $site_offers[$site->getId()][] = $offer;
                }
              }
            }
            break;
          case 2:
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseZumzi($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 3:
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseGroupon($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    $offer_city->delete();
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save();
                  
                  $site_offers[$site->getId()][] = $offer;
                }
              }
            }
            break;
          case 4: // goldendeals
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseGoldendeals($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save();
                  
                  $site_offers[$site->getId()][] = $offer;
                }
              }
            }
            break;
          case 5: // kuponiada
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseKuponiada($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    $offer_city->delete();
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save();
                  
                  $site_offers[$site->getId()][] = $offer;
                }
              }
            }
            break;
          case 6: // coolbuy
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseCoolbuy($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save();
                  
                  $site_offers[$site->getId()][] = $offer;
                }
              }
            }
            break;
          case 7: // topdeals
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseTopdeals($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save();
                  
                  $site_offers[$site->getId()][] = $offer;
                }
              }
            }
            break;
        }
      }
      
      //return $this->renderPartial('mail/cronSiteParser', array('offers' => $site_offers, 'time' => time() - $start_time));
      
      $bodyHtml = $this->getPartial('mail/cronSiteParser', array('offers' => $site_offers, 'time' => time() - $start_time));
      
      $mailer = $this->getMailer();
      $message = $mailer->compose('website@reduceri-cadouri.ro', 'admin@reduceri-cadouri.ro', 'Cron SiteParser');
      $message->setBody($bodyHtml,'text/html');
      $mailer->send($message);
    }
    
    return sfView::NONE;
  }
  
  public function executeParseSite(sfWebRequest $request)
  {
    set_time_limit(0);
    
    if($site_id = $request->getParameter('site_id'))
    {
      if($site = SitePeer::retrieveByPK($site_id))
      {
        $start_time = time();
        
        $site_offers = array();
        switch($site->getId())
        {
          case 1: // fundeal
            //fundeal national
            $offers = SiteParser::parseFundeal('http://www.fundeal.ro/national/');
            foreach($offers as $offer)
            {
              if($offer)
              {
                foreach($offer->getOfferCitys() as $offer_city)
                {
                  try
                  {
                    $offer_city->delete();
                  }
                  catch(Exception $e)
                  {
                    // var_dump($e->getMessage());
                  }
                }
                foreach(CityPeer::getAll() as $city)
                {
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($city->getId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                }
                $offer->setSiteId($site->getId());
                $offer->setOnSite(true);
                $offer->save(null, true);
                
                $site_offers[$site->getId()][$offer->getId()] = $offer;
              }
            }
            
            $urls = $site->getSiteUrls();
            shuffle($urls);
            foreach($urls as $site_url)
            {
              $offers = SiteParser::parseFundeal($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 2:
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseZumzi($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 3:
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseGroupon($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            
            //groupon national
            $offers = SiteParser::parseGroupon('http://www.groupon.ro/deals/national');
            foreach($offers as $offer)
            {
              if($offer)
              {
                foreach($offer->getOfferCitys() as $offer_city)
                {
                  try
                  {
                    $offer_city->delete();
                  }
                  catch(Exception $e)
                  {
                    // var_dump($e->getMessage());
                  }
                }
                foreach(CityPeer::getAll() as $city)
                {
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($city->getId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                }
                $offer->setSiteId($site->getId());
                $offer->setOnSite(true);
                $offer->save(null, true);
                
                $site_offers[$site->getId()][$offer->getId()] = $offer;
              }
            }
            break;
          case 4: // goldendeals
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseGoldendeals($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 5: // kuponiada
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseKuponiada($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 6: // coolbuy
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseCoolbuy($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 7: // topdeals
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseTopdeals($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 8: // citydays
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseCitydays($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 9: // ideals
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseIdeals($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 10: // perfectdeals
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parsePerfectdeals($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 11: // getadeal.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseGetadeal($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 12: // reddeal.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseReddeal($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 13: // e-redus.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseEredus($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 14: // oferta.noi2.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseNoi2($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 15: // profitam.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseProfitamro($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 16: // bestdealz.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseBestdealz($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 17: // qupon.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseQupon($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 18: // crazydeal.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseCrazydeal($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }

            //crazydeal national
            $offers = SiteParser::parseCrazydeal('http://www.crazydeal.ro/active/national/all');
            foreach($offers as $offer)
            {
              if($offer)
              {
                foreach($offer->getOfferCitys() as $offer_city)
                {
                  try
                  {
                    $offer_city->delete();
                  }
                  catch(Exception $e)
                  {
                    // var_dump($e->getMessage());
                  }
                }
                foreach(CityPeer::getAll() as $city)
                {
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($city->getId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                }
                $offer->setSiteId($site->getId());
                $offer->setOnSite(true);
                $offer->save(null, true);
                
                $site_offers[$site->getId()][$offer->getId()] = $offer;
              }
            }
            break;
            
          case 19: // fungroup.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseFungroup($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 20: // pandoria.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parsePandoria($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 21: // dealfever.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseDealfever($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 22: // reducerimaxime.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseReduceriMaxime($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            
            // oferte nationale
            $offers = SiteParser::parseReduceriMaxime('http://www.reducerimaxime.ro/nationale');
            // oferte online
            $offers = array_merge($offers, SiteParser::parseReduceriMaxime('http://www.reducerimaxime.ro/online'));
            $parsed_offers = array();
            foreach($offers as $offer)
            {
              if($offer && !in_array($offer->getId(), $parsed_offers))
              {
                $parsed_offers[] = $offer->getId();
                foreach($offer->getOfferCitys() as $offer_city)
                {
                  try
                  {
                    $offer_city->delete();
                  }
                  catch(Exception $e)
                  {
                    // var_dump($e->getMessage());
                  }
                }
                foreach(CityPeer::getAll() as $city)
                {
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($city->getId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                }
                $offer->setSiteId($site->getId());
                $offer->setOnSite(true);
                $offer->save(null, true);
                
                $site_offers[$site->getId()][$offer->getId()] = $offer;
              }
            }
            break;
            
          case 23: // therealdeal.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseTheRealDeal($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 24: // prodeal.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseProDeal($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 25: // waz.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseWaz($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  foreach(CityPeer::getAll() as $city)
                  {
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($city->getId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                  }
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
              
              break;
            }
            break;
            
          case 26: // pontomat.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parsePontomat($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 27: // promo-romania.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parsePromoRomania($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  foreach(CityPeer::getAll() as $city)
                  {
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($city->getId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                  }
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 28: // savemoney.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseSaveMoney($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 29: // localdeals.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseLocalDeals($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 30: // gogetit.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseGogetit($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 31: // wisebuy.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseWisebuy($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 32: // oferteiasi.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseOferteiasi($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 33: // ofertapret.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseOfertapret($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 34: // shopdeals.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseShopdeals($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 35: // ofertadelux.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseOfertadelux($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 36: // kupedia.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseKupedia($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 37: // dealmania.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseDealmania($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 38: // buyzilla.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseBuyzilla($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 39: // reduceri-oferte.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseReduceriOferte($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
			
          case 40: // easydeals.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseEasyDeals($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
        }
        
        $current_offers = $site->getCurrentOffers(date('Y-m-d H:i:s'));
        foreach($current_offers as $key => $current_offer)
        {
          //echo '<br />Searching: '.$current_offer->getName()."<br />\n";
          $found = false;
          foreach($site_offers[$site->getId()] as $offer)
          {
            if($current_offer->getId() == $offer->getId())
            {
              $found = true;
            }
          }
          if(!$found)
          {
            $current_offer->setEndTime(date('Y-m-d 00:00:00'));
            $current_offer->save();
            //echo 'Not found: '.$current_offer->getName()."<br />\n";
          }
        }
        
        foreach($site_offers[$site->getId()] as $offer)
        {
          $offer->share();
        }
        
        /*
        $bodyHtml = $this->getPartial('mail/cronSiteParser', array('offers' => $site_offers, 'time' => time() - $start_time));
        
        $mailer = $this->getMailer();
        $message = $mailer->compose('website@reduceri-cadouri.ro', 'admin@reduceri-cadouri.ro', 'Cron SiteParser '.$site->getName());
        $message->setBody($bodyHtml,'text/html');
        $mailer->send($message);
        */
      }
    }
    
    return sfView::NONE;
  }

  public function executeParseSite2(sfWebRequest $request)
  {
    set_time_limit(0);
    
    if(CronParserPeer::isRunning())
    {
      return sfView::NONE;
    }
    
    $start_time = time();
    
    $last_site_id = null;
    $next_site_id = null;
    if($last_cron_parser = CronParserPeer::getLastCronParser())
    {
      $last_site_id = $last_cron_parser->getSiteId();
    }
    
    $sites = SitePeer::getAllActiveForParser();
    
    if(!$last_site_id)
    {
      foreach($sites as $site)
      {
        $next_site_id = $site->getId();
        break;
      }
    }
    else
    {
      $next = false;  
      foreach($sites as $site)
      {
        if($last_site_id == $site->getId())
        {
          $next = true;
          continue;
        }
        if($next)
        {
          $next_site_id = $site->getId();
          break;
        }
      }
    }
    if(!$next_site_id)
    {
      foreach($sites as $site)
      {
        $next_site_id = $site->getId();
        break;
      }
    }
    
    $cron_parser = new CronParser();
    $cron_parser->setSiteId($next_site_id);
    $cron_parser->save();
    
    foreach($sites as $site)
    {
      if($site->getId() == $next_site_id)
      {
        $site_offers = array();
        switch($site->getId())
        {
          case 1: // fundeal
            //fundeal national
            $offers = SiteParser::parseFundeal('http://www.fundeal.ro/national/');
            foreach($offers as $offer)
            {
              if($offer)
              {
                foreach($offer->getOfferCitys() as $offer_city)
                {
                  try
                  {
                    $offer_city->delete();
                  }
                  catch(Exception $e)
                  {
                    // var_dump($e->getMessage());
                  }
                }
                foreach(CityPeer::getAll() as $city)
                {
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($city->getId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                }
                $offer->setSiteId($site->getId());
                $offer->setOnSite(true);
                $offer->save(null, true);
                
                $site_offers[$site->getId()][$offer->getId()] = $offer;
              }
            }
            
            $urls = $site->getSiteUrls();
            shuffle($urls);
            foreach($urls as $site_url)
            {
              $offers = SiteParser::parseFundeal($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 2:
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseZumzi($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 3:
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseGroupon($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            
            //groupon national
            $offers = SiteParser::parseGroupon('http://www.groupon.ro/deals/national');
            foreach($offers as $offer)
            {
              if($offer)
              {
                foreach($offer->getOfferCitys() as $offer_city)
                {
                  try
                  {
                    $offer_city->delete();
                  }
                  catch(Exception $e)
                  {
                    // var_dump($e->getMessage());
                  }
                }
                foreach(CityPeer::getAll() as $city)
                {
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($city->getId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                }
                $offer->setSiteId($site->getId());
                $offer->setOnSite(true);
                $offer->save(null, true);
                
                $site_offers[$site->getId()][$offer->getId()] = $offer;
              }
            }
            break;
          case 4: // goldendeals
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseGoldendeals($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 5: // kuponiada
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseKuponiada($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 6: // coolbuy
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseCoolbuy($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 7: // topdeals
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseTopdeals($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 8: // citydays
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseCitydays($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 9: // ideals
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseIdeals($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 10: // perfectdeals
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parsePerfectdeals($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 11: // getadeal.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseGetadeal($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 12: // reddeal.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseReddeal($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 13: // e-redus.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseEredus($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 14: // oferta.noi2.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseNoi2($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
          case 15: // profitam.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseProfitamro($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 16: // bestdealz.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseBestdealz($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 17: // qupon.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseQupon($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 18: // crazydeal.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseCrazydeal($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }

            //crazydeal national
            $offers = SiteParser::parseCrazydeal('http://www.crazydeal.ro/active/national/all');
            foreach($offers as $offer)
            {
              if($offer)
              {
                foreach($offer->getOfferCitys() as $offer_city)
                {
                  try
                  {
                    $offer_city->delete();
                  }
                  catch(Exception $e)
                  {
                    // var_dump($e->getMessage());
                  }
                }
                foreach(CityPeer::getAll() as $city)
                {
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($city->getId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                }
                $offer->setSiteId($site->getId());
                $offer->setOnSite(true);
                $offer->save(null, true);
                
                $site_offers[$site->getId()][$offer->getId()] = $offer;
              }
            }
            break;
            
          case 19: // fungroup.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseFungroup($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 20: // pandoria.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parsePandoria($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 21: // dealfever.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseDealfever($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 22: // reducerimaxime.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseReduceriMaxime($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            
            // oferte nationale
            $offers = SiteParser::parseReduceriMaxime('http://www.reducerimaxime.ro/nationale');
            // oferte online
            $offers = array_merge($offers, SiteParser::parseReduceriMaxime('http://www.reducerimaxime.ro/online'));
            $parsed_offers = array();
            foreach($offers as $offer)
            {
              if($offer && !in_array($offer->getId(), $parsed_offers))
              {
                $parsed_offers[] = $offer->getId();
                foreach($offer->getOfferCitys() as $offer_city)
                {
                  try
                  {
                    $offer_city->delete();
                  }
                  catch(Exception $e)
                  {
                    // var_dump($e->getMessage());
                  }
                }
                foreach(CityPeer::getAll() as $city)
                {
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($city->getId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                }
                $offer->setSiteId($site->getId());
                $offer->setOnSite(true);
                $offer->save(null, true);
                
                $site_offers[$site->getId()][$offer->getId()] = $offer;
              }
            }
            break;
            
          case 23: // therealdeal.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseTheRealDeal($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 24: // prodeal.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseProDeal($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 25: // waz.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseWaz($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  foreach(CityPeer::getAll() as $city)
                  {
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($city->getId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                  }
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
              
              break;
            }
            break;
            
          case 26: // pontomat.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parsePontomat($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 27: // promo-romania.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parsePromoRomania($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  foreach(CityPeer::getAll() as $city)
                  {
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($city->getId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                  }
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 28: // savemoney.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseSaveMoney($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 29: // localdeals.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseLocalDeals($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 30: // gogetit.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseGogetit($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 31: // wisebuy.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseWisebuy($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 32: // oferteiasi.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseOferteiasi($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 33: // ofertapret.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseOfertapret($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 34: // shopdeals.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseShopdeals($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 35: // ofertadelux.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseOfertadelux($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 36: // kupedia.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseKupedia($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 37: // dealmania.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseDealmania($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 38: // buyzilla.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseBuyzilla($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 39: // reduceri-oferte.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseReduceriOferte($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  $offer_city = new OfferCity();
                  $offer_city->setCityId($site_url->getCityId());
                  $offer_city->setOfferId($offer->getId());
                  $offer_city->save();
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
            
          case 40: // easydeals.ro
            foreach($site->getSiteUrls() as $site_url)
            {
              $offers = SiteParser::parseEasyDeals($site_url->getUrl());
              foreach($offers as $offer)
              {
                if($offer)
                {
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
            }
            break;
        }
        
        $current_offers = $site->getCurrentOffers(date('Y-m-d H:i:s'));
        foreach($current_offers as $key => $current_offer)
        {
          //echo '<br />Searching: '.$current_offer->getName()."<br />\n";
          $found = false;
          foreach($site_offers[$site->getId()] as $offer)
          {
            if($current_offer->getId() == $offer->getId())
            {
              $found = true;
            }
          }
          if(!$found)
          {
            if(!$current_offer->getPromo())
            {
              $current_offer->setEndTime(date('Y-m-d 00:00:00'));
              $current_offer->save();
              //echo 'Not found: '.$current_offer->getName()."<br />\n";
            }
          }
        }
        
        foreach($site_offers[$site->getId()] as $offer)
        {
          $offer->share();
        }
      }
    }

    $cron_parser->setDuration(time() - $start_time);
    $cron_parser->save();
    
    return sfView::NONE;
  }
  
  public function executePublishShare(sfWebRequest $request)
  {
    if(date('H') >= 7 && date('H') <= 18)
    {
      if($share = SocialSharePeer::getShareToPublish('facebook'))
      {
        $share->publish();
      }
      if($share = SocialSharePeer::getShareToPublish('twitter'))
      {
        $share->publish();
      }
    }
    return sfView::NONE;
  }
  
  public function executeGenerateSitemap(sfWebRequest $request)
  {
    $sitemap = new DOMDocument('1.0', 'utf-8');
    $sitemap->formatOutput = true;
    $sitemap->encoding = "utf-8";
    $url_set = $sitemap->appendChild(new DOMElement('urlset'));
    $attribute_url_set = $url_set->setAttributeNode(new DOMAttr('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9'));
    
    // homepage url
    $url = $url_set->appendChild(new DOMElement('url'));
    $location = $url->appendChild(new DOMElement('loc', $this->getController()->genUrl('@homepage', true)));
    $priority = $url->appendChild(new DOMElement('priority', '1'));
    $change_frequency = $url->appendChild(new DOMElement('changefreq', 'daily'));
    
    // cities
    $cities = CityPeer::getAll();
    foreach($cities as $city)
    {
      $url = $url_set->appendChild(new DOMElement('url'));
      $location = $url->appendChild(new DOMElement('loc', $this->getController()->genUrl('@city?city_name='.Utils::slugify($city->getName()), true)));
      $priority = $url->appendChild(new DOMElement('priority', '0.9'));
      $change_frequency = $url->appendChild(new DOMElement('changefreq', 'daily'));
    }

    // categories
    $categories = CategoryPeer::getAll();
    foreach($categories as $category)
    {
      $url = $url_set->appendChild(new DOMElement('url'));
      $location = $url->appendChild(new DOMElement('loc', $this->getController()->genUrl('@city?city_name=all&category_name='.Utils::slugify($category->getName()), true)));
      $priority = $url->appendChild(new DOMElement('priority', '0.8'));
      $change_frequency = $url->appendChild(new DOMElement('changefreq', 'daily'));
    }
    
    // offers
    $c = new Criteria();
    $c->add(OfferPeer::ON_SITE, 1);
    $c->add(OfferPeer::START_TIME, date("Y-m-d H:i:s"), Criteria::LESS_EQUAL);
    $c->add(OfferPeer::END_TIME, date("Y-m-d H:i:s"), Criteria::GREATER_THAN);
    $c->addDescendingOrderByColumn(OfferPeer::DISCOUNT_PERCENT);
    $offers = OfferPeer::doSelect($c);
    foreach($offers as $offer)
    {
      $url = $url_set->appendChild(new DOMElement('url'));
      $location = $url->appendChild(new DOMElement('loc', $this->getController()->genUrl($offer->getRouteName2().'?id='.$offer->getId().'&name='.Utils::slugify($offer->getName()), true)));
      $priority = $url->appendChild(new DOMElement('priority', '0.6'));
    }
    
    $sitemap->save(sfConfig::get('sf_web_dir').'/sitemap.xml');
    
    return sfView::NONE;
  }
  
  public function executeSendOffersEmail(sfWebRequest $request)
  {
    $bodyHtml = '';
    $count = 0;
    $results = array();
    
    $c = new Criteria();
    $c->add(SitePeer::IS_ACTIVE, 1);
    $sites = SitePeer::doSelect($c);
    foreach($sites as $site)
    {
      $c = new Criteria();
      $c->add(OfferPeer::SITE_ID, $site->getId());
      $c->add(OfferPeer::ON_SITE, 1);
      $c->add(OfferPeer::START_TIME, date("Y-m-d H:i:s"), Criteria::LESS_EQUAL);
      $c->add(OfferPeer::END_TIME, date("Y-m-d H:i:s"), Criteria::GREATER_THAN);
      $offers = OfferPeer::doCount($c);      
      $results[$site->getName()] = $offers;
      
      $count += $offers;
    }
    
    arsort($results);
    foreach($results as $siteName => $result)
    {
      $bodyHtml .= '<b>'.$siteName.'</b> - '.$result.' oferte <br / >';
    }
    
    $bodyHtml = '<b>Total:</b> '.$count.' oferte<br /><br />'.$bodyHtml;

    $mailer = $this->getMailer();
    $message = $mailer->compose('website@reduceri-cadouri.ro', 'admin@reduceri-cadouri.ro', 'Cron SiteParser');
    $message->setBody($bodyHtml,'text/html');
    $mailer->send($message);
        
    return sfView::NONE;
  }
  
  public function executeCleanSearch(sfWebRequest $request)
  {
    set_time_limit(0);
    
    ProjectConfiguration::registerZend();
    $index = OfferPeer::getLuceneIndex();
    $i = 0;
    $c = new Criteria();
    $c->add(OfferPeer::IN_SEARCH, 1);
    $offers = OfferPeer::doSelect($c);
    foreach($offers as $offer)
    {
      if(!$offer->isActive())
      {
        foreach($index->find('pk:'.$offer->getId()) as $hit)
        {
          $index->delete($hit->id);
          $i++;
        }
        $offer->setInSearch(false);
        $offer->save();
      }
    }
    
    $bodyHtml = 'Deleted '.$i.' records from search';
    
    /*
    $c = new Criteria();
    $c->add(OfferPeer::END_TIME, date("Y-m-d H:i:s", strtotime('-60 days')), Criteria::LESS_EQUAL);
    $c->addAscendingOrderByColumn(OfferPeer::CREATED_AT);
    $c->setLimit(100);
    $offers = OfferPeer::doSelect($c);
    foreach($offers as $offer)
    {
      $offer->delete();
    }
    */
    
    // $bodyHtml .= "\n".'Deleted '.count($offers).' old offers';
    
    $mailer = $this->getMailer();
    $message = $mailer->compose('website@reduceri-cadouri.ro', 'admin@reduceri-cadouri.ro', 'Cron CleanSearch');
    $message->setBody($bodyHtml,'text/html');
    $mailer->send($message);
        
    return sfView::NONE;
  }

  public function executeClearImages(sfWebRequest $request)
  {
    $sql = "SELECT offer.id, COUNT(offer_image.id) as nr FROM offer, offer_image WHERE offer_image.offer_id = offer.id GROUP BY offer.id ORDER BY nr DESC";
    $connection = Propel::getConnection();
    $statement = $connection->prepare($sql);
    $statement->execute();
    $ids = array();
    while($resultset = $statement->fetch(PDO::FETCH_OBJ))
    {
      if($resultset->nr > 1)
      {  
        $ids[] = $resultset->id;
      }
    }
    
    $image_ids = array();
    
    foreach($ids as $id)
    {
      $sql = "SELECT id, image FROM offer_image WHERE offer_id = ".$id;
      $connection = Propel::getConnection();
      $statement = $connection->prepare($sql);
      $statement->execute();
      $i = 0;
      while($resultset = $statement->fetch(PDO::FETCH_OBJ))
      {
        if($i)
        {
          if(file_exists(sfConfig::get('sf_upload_dir').'/offers/small/'.$resultset->image))
          {
            unlink(sfConfig::get('sf_upload_dir').'/offers/small/'.$resultset->image);
          }
          $image_ids[] = $resultset->id;
        }
        
        $i++;
      }
    }
    
    foreach($image_ids as $id)
    {
      $sql = "DELETE FROM offer_image WHERE id = ".$id;
      $connection = Propel::getConnection();
      $statement = $connection->prepare($sql);
      $statement->execute();
    }
     
    var_dump($ids);
    
    return sfView::NONE;
  }
  
  public function executeParseEmag(sfWebRequest $request)
  {
    set_time_limit(0);
    
    EmagParser::parseEmag();
    
    return sfView::NONE;
  }
}
